const mongoose = require("mongoose");

// 获取用户全部数据
const get = async ({ userId, genre }) => {
    let set = [];
    let num = [];
    const commsModel = mongoose.model("comms");
    const orderModel = mongoose.model("order");
    // 获取全部数据
    console.log(userId, genre);
    const data = await orderModel.find().populate("userId");
    for (let i = 0; i < data.length; i++) {
        if (data[i].userId._id == userId) {
            set.push(data[i])
        }
    };
    console.log(data);
    // 根据动态遍历数据
    if (genre === "已完成" || genre === "待发货" || genre === "待收货") {
        for (let i = 0; i < data.length; i++) {
            if (data[i].state == genre) {
                num.push(data[i])
            }
        }
        for (let j = 0; j < num.length; j++) {
            for (let x = 0; x < num[j].information.length; x++) {
                num[j].information[x].commId = await commsModel.findOne({ _id: num[j].information[x].commId });
            }
        }
        return num
    }
    for (let j = 0; j < set.length; j++) {
        for (let x = 0; x < set[j].information.length; x++) {
            console.log(set[j].information[x].commId,2);
            set[j].information[x].commId = await commsModel.findOne({ _id: set[j].information[x].commId });
        }
    }
    console.log(set,1);
    return set
};
// 更新订单状态
const put = async ({ Id }) => {
    const orderModel = mongoose.model("order");
    const data = await orderModel.findByIdAndUpdate({ _id: Id }, { state: "已完成" });
    if (data) {
        return true;
    }
    return false;
}
// 判断是否有自提点
const address = async ({ userId }) => {
    console.log(userId);
    const orderModel = mongoose.model("order");
    const data = await orderModel.find().populate('userId');
    let set = [];
    for (let i = 0; i < data.length; i++) {
        if (data[i].userId._id == userId) {
            set.push(data[i])
        }
    };
    for (let j = 0; j < set.length; j++) {
        console.log(set[j].userId.address);
        if (set[j].userId.address === []) {
            return false
        } else {
            return true
        }

    }
}
// 删除订单
const deleteId = async ({ Id }) => {
    const orderModel = mongoose.model("order");
    console.log(Id);
    const data = await orderModel.remove({ _id: Id });
    console.log(data);
}

// 新增订单并且删除库存  判断库存是否足够
const post = async ({ userId, information, total, time, state }) => {
    const orderModel = mongoose.model("order");
    // 获取商品数据模型
    const commsModel = mongoose.model("comms");
    const shoppingModel = mongoose.model("shopping");
    for (let i = 0; i < information.length; i++) {
        const comm = await commsModel.findOne({ _id: information[i].commId });
        // 判断库存是否足够
        if (comm.count == information[i].quantity || comm.count > information[i].quantity) {
            // 减去库存并更新
            let count = comm.count - information[i].quantity;
            await commsModel.findByIdAndUpdate({ _id: information[i].commId }, { count });
            await shoppingModel.findOneAndDelete({ shoppingid: information[i].commId });
        } else if (comm.count < information[i].quantity) {
            return false
        }
        // 删除库存
    }
    await orderModel.create({ userId, information, total, time, state });
    return true
}
module.exports = { get, put, address, deleteId, post }